{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "闻:什么都没有闻到\n"
     ]
    }
   ],
   "source": [
    "# 模拟“听”和“闻”信号\n",
    "signals = {\"嗅觉信号\":\"闻\",\"听觉信号\":\"听\"}\n",
    "class Neuron(object):\n",
    "    def __init__(self,signal_type):\n",
    "        #模拟神经元处理某种信号的能力，signal_type为突触携带的参数\n",
    "        self.signal_type = signal_type\n",
    "    def active(self,x):\n",
    "        #输入某种类型的信号，有可能激活神经元响应刺激\n",
    "        if x.signal_type == self.signal_type:\n",
    "            return signals[self.signal_type]+\":\"+x.data\n",
    "        else:\n",
    "            return signals[self.signal_type]+\":\"+\"什么都没有\"+signals[self.signal_type]+\"到\"\n",
    "        \n",
    "class SignalInput(object):\n",
    "    #输入信息号\n",
    "    def __init__(self,signal_type,data):\n",
    "        self.signal_type = signal_type\n",
    "        self.data = data\n",
    "test = SignalInput(\"视觉信号\",\"臭豆腐好香呀！\")\n",
    "print(Neuron(\"嗅觉信号\").active(test))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 脑区功能划分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "听觉皮层正在处理：好美的歌！\n",
      "嗅觉皮层正在处理：臭豆腐真香，想吃！\n"
     ]
    }
   ],
   "source": [
    "from collections import Counter\n",
    "from random import randint\n",
    "random.seed(1314)\n",
    "class HearBrainArea(object):\n",
    "    #听觉皮层脑区\n",
    "    def __init__(self,num):\n",
    "        self.neurons = [Neuron(\"听觉信号\") for i in range(num)]\n",
    "    def process(self,x):\n",
    "        #处理传入的信号\n",
    "        print(\"听觉皮层正在处理：\"+x.data)\n",
    "class SmellBrainArea(object):\n",
    "    #嗅觉皮层\n",
    "    def __init__(self,num):\n",
    "        self.neurons = [Neuron(\"嗅觉信号\") for i in range(num)]\n",
    "    def process(self,x):\n",
    "        #处理信号\n",
    "        print(\"嗅觉皮层正在处理：\"+x.data)\n",
    "#构建器官，嘴巴和耳朵，将接受的信号分配给专门的大脑皮层进行处理\n",
    "class Brain(object):\n",
    "    def __init__(self):\n",
    "        #控制神经元数量，随机初始化\n",
    "        self.hear_area = HearBrainArea(randint(1000,10000))\n",
    "        self.smell_area = SmellBrainArea(randint(1000,10000))\n",
    "    def process(self,x):\n",
    "        result = {\"嗅觉信号\":lambda x:self.smell_area.process(x),\n",
    "                  \"听觉信号\":lambda x:self.hear_area.process(x)}[x.signal_type](x)\n",
    "brain = Brain()\n",
    "hear = SignalInput(\"听觉信号\",\"好美的歌！\")\n",
    "smell = SignalInput(\"嗅觉信号\",\"臭豆腐真香，想吃！\")\n",
    "brain.process(hear)\n",
    "brain.process(smell)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 相同神经元投票决定输出信号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "听觉皮层正在处理：小黄鹂在唱歌\n",
      "耳多听【听觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：臭豆腐闻起来真臭\n",
      "鼻子闻【嗅觉皮层】：闻:臭豆腐闻起来真臭\n"
     ]
    }
   ],
   "source": [
    "from collections import Counter\n",
    "from random import randint\n",
    "random.seed(1314)\n",
    "class HearBrainArea(object):\n",
    "    #听觉皮层脑区\n",
    "    def __init__(self,num):\n",
    "        self.neurons = [Neuron(\"听觉信号\") for i in range(num)]\n",
    "    def process(self,x):\n",
    "        #处理传入的信号\n",
    "        print(\"听觉皮层正在处理：\"+x.data)\n",
    "        #所有神经元的动作集合\n",
    "        actions = [neuron.active(x) for neuron in self.neurons]\n",
    "        #从所有动作中投票决定最终响应\n",
    "        final_action = Counter(actions).most_common(1)\n",
    "        print(\"耳多听【听觉皮层】：\"+final_action[0][0])\n",
    "class SmellBrainArea(object):\n",
    "    #嗅觉皮层\n",
    "    def __init__(self,num):\n",
    "        self.neurons = [Neuron(\"嗅觉信号\") for i in range(num)]\n",
    "    def process(self,x):\n",
    "        #处理信号\n",
    "        print(\"嗅觉皮层正在处理：\"+x.data)\n",
    "        #所有神经元的动作集合\n",
    "        actions = [neuron.active(x) for neuron in self.neurons]\n",
    "        #从所有动作中投票决定最终响应\n",
    "        final_action = Counter(actions).most_common(1)\n",
    "        print(\"鼻子闻【嗅觉皮层】：\"+final_action[0][0])\n",
    "#模拟器官接受到的信号\n",
    "brain = Brain()\n",
    "hear = SignalInput(\"听觉信号\",\"小黄鹂在唱歌\")\n",
    "smell = SignalInput(\"嗅觉信号\",\"臭豆腐闻起来真臭\")\n",
    "brain.process(hear)\n",
    "brain.process(smell)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 大脑皮层的可替换性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：闻:什么都没有闻到\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：闻:什么都没有闻到\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：闻:什么都没有闻到\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：闻:什么都没有闻到\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：闻:什么都没有闻到\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：闻:什么都没有闻到\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：闻:什么都没有闻到\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：闻:什么都没有闻到\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：闻:什么都没有闻到\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：闻:什么都没有闻到\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：闻:什么都没有闻到\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：闻:什么都没有闻到\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：闻:什么都没有闻到\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n",
      "嗅觉皮层正在处理：小黄鹂在唱歌\n",
      "鼻子闻【嗅觉皮层】：听:小黄鹂在唱歌\n"
     ]
    }
   ],
   "source": [
    "class Neuron(object):\n",
    "    def __init__(self,signal_type):\n",
    "        #模拟神经元处理某种信号的能力，signal_type为突触携带的参数\n",
    "        self.signal_type = signal_type\n",
    "    def active(self,x):\n",
    "        #输入某种类型的信号，有可能激活神经元响应刺激,假设有5%的几率被训练改变\n",
    "        if(random.random() <= 0.05):\n",
    "            self.signal_type = x.signal_type\n",
    "        if x.signal_type == self.signal_type:\n",
    "            return signals[self.signal_type]+\":\"+x.data\n",
    "        else:\n",
    "            return signals[self.signal_type]+\":\"+\"什么都没有\"+signals[self.signal_type]+\"到\"\n",
    "        \n",
    "#连续输入100次，看看神经元处理类型的变化\n",
    "brain = Brain()\n",
    "hear = SignalInput(\"听觉信号\",\"小黄鹂在唱歌\")\n",
    "for i in range(100):\n",
    "    brain.smell_area.process(hear)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
